/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cashdevice_stub.h"
#include <string.h>
#include "message_option.h"
#include "message_parcel.h"
#include "hilog_wrapper.h"
#include "cashdevice_common.h"
#include "cashdevice_errors.h"
#include "cashdevice_def.h"
#include "parcelable_vector_rw.h"

namespace SwanLink {
namespace Finance {
int CashDeviceSrvStub::OnRemoteRequest(uint32_t code,  MessageParcel& data,
    MessageParcel& reply,  MessageOption& option)
{
    COMMON_HILOGD(COMMON_SRV,  "cmd = %{public}d,  flags = %{public}d",  code,  option.GetFlags());

    return CheckRequestCode(code,  data,  reply,  option);
}

int32_t CashDeviceSrvStub::CheckRequestCode(const uint32_t code,  MessageParcel& data,  MessageParcel& reply,
    MessageOption& option)
{
    switch (code) {
        case static_cast<int>(ICashDeviceSrv::CASHDEVICE_GETSUPPORTACCESS): {
            return GetSupportAccessStub(data, reply, option);
        }
        case static_cast<int>(ICashDeviceSrv::CASHDEVICE_OPEN): {
            return OpenStub(data, reply, option);
        }
        case static_cast<int>(ICashDeviceSrv::CASHDEVICE_CLOSE): {
            return CloseStub(data, reply, option);
        }
        case static_cast<int>(ICashDeviceSrv::CASHDEVICE_QUERYSTATUS): {
            return QueryStatusStub(data, reply, option);
        }
        case static_cast<int>(ICashDeviceSrv::CASHDEVICE_RESET): {
            return ResetStub(data, reply, option);
        }
    }
    return ERR_OK;
}

int32_t CashDeviceSrvStub::GetSupportAccessStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    std::vector<DevAccess> result = GetSupportAccess();
    if (!WriteVectorToParcel(reply, result)) {
        COMMON_HILOGE(COMMON_SRV, "WriteVector failed, size = %{public}d", (int)result.size());
        return E_WRITE_PARCEL_ERROR;
    }
    return ERR_OK;
}
int32_t CashDeviceSrvStub::OpenStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    std::string name;
    Transport *trans;

    READ_PARCEL_WITH_RET(data, String, name, E_READ_PARCEL_ERROR);
    READ_BUFFER_WITH_RET(data, Transport, trans, sizeof(Transport), E_READ_PARCEL_ERROR);

    int result = Open(name, *trans);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}
int32_t CashDeviceSrvStub::CloseStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);

    int result = Close(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}
int32_t CashDeviceSrvStub::QueryStatusStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int module = 0; 
    char err[256];

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);
    READ_PARCEL_WITH_RET(data, Int32, module, E_READ_PARCEL_ERROR);
    int result = QueryStatus(handle, module, err);
    WRITE_PARCEL_WITH_RET(reply, String, err, E_WRITE_PARCEL_ERROR);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}
int32_t CashDeviceSrvStub::ResetStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, E_READ_PARCEL_ERROR);

    int result = Reset(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, E_WRITE_PARCEL_ERROR);

    return ERR_OK;
}
}  // namespace Finance
}  // namespace SwanLink
